home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / pp / pp-6.0 / Lib / table / tb_k2val.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-18  |  3.5 KB  |  192 lines

  1. /* tb_k2val.c: primitive routine - convert key to value */
  2.  
  3. # ifndef lint
  4. static char Rcsid[] = "@(#)$Header: /xtel/pp/pp-beta/Lib/table/RCS/tb_k2val.c,v 6.0 1991/12/18 20:24:28 jpo Rel $";
  5. # endif
  6.  
  7. /*
  8.  * $Header: /xtel/pp/pp-beta/Lib/table/RCS/tb_k2val.c,v 6.0 1991/12/18 20:24:28 jpo Rel $
  9.  *
  10.  * $Log: tb_k2val.c,v $
  11.  * Revision 6.0  1991/12/18  20:24:28  jpo
  12.  * Release 6.0
  13.  *
  14.  */
  15.  
  16.  
  17.  
  18. #include "util.h"
  19. #include "table.h"
  20.  
  21.  
  22. #define MAXF    80
  23.  
  24.  
  25. /*
  26. All the tables with open files
  27. */
  28.  
  29. static Table            *tb_xall [MAXF];
  30. static int              curclose;
  31. extern  char            *tbldfldir;
  32. static int tblosearch ();
  33.  
  34.  
  35.  
  36. /* ---------------------  Start  Routines  -------------------------------- */
  37.  
  38. int tb_k2val (tbl, key, val, first)
  39. register Table  *tbl;
  40. char            *key;
  41. char            *val;
  42. int        first;
  43. {
  44.     FILE    *f;
  45.     char    linebuf[LINESIZE],
  46.         value[LINESIZE];
  47.     int     i, status;
  48.  
  49.  
  50.     PP_DBG (("Lib/tb_k2val (%s)", key));
  51.  
  52.     if (tbl -> tb_override &&
  53.         tblosearch (tbl, key, val, first) == OK)
  54.         return OK;
  55.  
  56.     if (TB_SRC (tbl -> tb_flags) == TB_DBM)
  57.         return (tb_dbmk2val (tbl, key, val, first));
  58.  
  59.  
  60.     if (tbl -> tb_fp == NULLFILE) {
  61.         /*
  62.         Must open the file....
  63.         */
  64.         if (*tbl -> tb_file != '/') {
  65.             (void) sprintf (linebuf, "%s/%s",
  66.                     tbldfldir, tbl -> tb_file);
  67.             f = fopen (linebuf, "r");
  68.         }
  69.         else
  70.             f = fopen (tbl -> tb_file, "r");
  71.  
  72.         if (f == NULLFILE) {
  73.             PP_OPER (tbl -> tb_file, ("Cannot open"));
  74.             return (NOTOK);
  75.         }
  76.  
  77.         tbl -> tb_fp = f;
  78.  
  79.         for (i = 0 ; i < MAXF ; i++)
  80.             if (tb_xall [(i+curclose) % MAXF] == NULL) {
  81.                 tb_xall [(i+curclose) % MAXF] = tbl;
  82.                 goto got;
  83.             }
  84.  
  85.         (void) fclose (tb_xall [curclose] -> tb_fp);
  86.         tb_xall [curclose] -> tb_fp = NULLFILE;
  87.         tb_xall [curclose++] = tbl;
  88.         curclose %= MAXF;
  89.  
  90.     got:;
  91.     }
  92.  
  93.     f = tbl -> tb_fp;
  94.     if (first)
  95.         rewind (f);
  96.     while ((status = tab_fetch (f, linebuf, value)) != DONE) {
  97.         if (status == NOTOK)
  98.             continue;
  99.         if (lexequ (linebuf, key) == 0) {
  100.             /*
  101.             Found now return it
  102.             */
  103.             if (val == NULL)
  104.                 return (OK);
  105.             (void) strcpy (val, value);
  106.             return (OK);
  107.         }
  108.     }
  109.     return (NOTOK);
  110. }
  111.  
  112.  
  113. int tab_fetch (f, key, val)
  114. register FILE           *f;
  115. char                    *key;
  116. char                    *val;
  117. {
  118.     register char *cp;
  119.     int    c;
  120.  
  121.     while ((c = getc(f)) != EOF && isspace (c))
  122.         continue;
  123.  
  124.     if (c == '#') {
  125.         while ((c = getc(f)) != EOF && c != '\n')
  126.             continue;
  127.         return tab_fetch (f, key, val);
  128.     }
  129.  
  130.     cp = key;
  131.     do {
  132.         if (c == '\n') {
  133.             *cp = '\0';
  134.             PP_LOG (LLOG_EXCEPTIONS, 
  135.                 ("no value - missing ':' key=%s", key));
  136.             return NOTOK;
  137.         }
  138.         if (c == ':') {
  139.             if (cp > key && cp[-1] == '\\')
  140.                 cp [-1] = c;
  141.             else    break;
  142.         }
  143.         else *cp ++ = c;
  144.         if (key - cp > LINESIZE) {
  145.             *cp = '\0';
  146.             PP_LOG (LLOG_EXCEPTIONS, ("Key too long '%s'", key));
  147.             return NOTOK;
  148.         }
  149.     } while ((c = getc (f)) != EOF);
  150.     if (c == EOF)
  151.         return DONE;
  152.     *cp = '\0';
  153.  
  154.     while ((c = getc(f)) != EOF && c != '\n' && isspace (c))
  155.         continue;
  156.  
  157.     cp = val;
  158.     do {
  159.         if (c == '\n')
  160.             break;
  161.         else 
  162.             *cp ++ = c;
  163.         if (val - cp > LINESIZE) {
  164.             *cp = '\0';
  165.             PP_LOG (LLOG_EXCEPTIONS, ("Value too long %s:%s",
  166.                         key, val));
  167.             return NOTOK;
  168.         }
  169.     } while ((c = getc (f)) != EOF);
  170.     if (c == EOF)
  171.         return DONE;
  172.     *cp = '\0';
  173.     return OK;
  174. }
  175.  
  176. /* ARGSUSED */
  177. static int  tblosearch (tbl, key, val, first)
  178. Table *tbl;
  179. char *key;
  180. char *val;
  181. int first;            /* may do something with this someday */
  182. {
  183.     TableOverride *to;
  184.  
  185.     for (to = tbl -> tb_override; to; to = to -> tbo_next)
  186.         if (lexequ (key, to -> tbo_key) == 0) {
  187.             (void) strcpy (val, to -> tbo_value);
  188.             return OK;
  189.         }
  190.     return NOTOK;
  191. }
  192.